package com.cmes.jdbc;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusPropertiesCustomizer;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.cmes.jdbc.plugins.AdminMetaObjectHandler;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;

/**
 * JDBC 自动配置类
 *
 * @author Bruce.Gong
 * @since 1.0.0-SNAPSHOT
 */
@AutoConfigureAfter({MybatisPlusAutoConfiguration.class})
@ConditionalOnClass(MybatisConfiguration.class)
public class JdbcModuleAutoConfiguration {

    /**
     * 注册自动填充
     *
     * @return MetaObjectHandler
     */
    @Bean
    @ConditionalOnMissingBean
    public MetaObjectHandler metaObjectHandler() {
        return new AdminMetaObjectHandler();
    }

    /**
     * 配置 MybatisPlus 插件
     * <p>
     * 1.分页插件
     *
     * <pre>
     * 属性名     | 类型     | 默认值 | 描述
     * overflow  | boolean  | false | 溢出总页数后是否进行处理
     * maxLimit  | Long     |       | 单页分页条数限制
     * dbType    | DbType   |       | 数据库类型
     * dialect   | IDialect |       | 方言实现类
     * </pre>
     * <p>
     * 默认使用 MySql 数据库
     *
     * @return MybatisPlusInterceptor
     */
    @Bean
    @ConditionalOnMissingBean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        PaginationInnerInterceptor paginationInterceptor = new PaginationInnerInterceptor();
        paginationInterceptor.setDbType(DbType.MYSQL);
        paginationInterceptor.setMaxLimit(1000L);
        // 防止全表更新与删除
        BlockAttackInnerInterceptor blockAttackInterceptor = new BlockAttackInnerInterceptor();

        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(paginationInterceptor);
        interceptor.addInnerInterceptor(blockAttackInterceptor);
        return interceptor;
    }

    @Bean
    public ConfigurationCustomizer configurationCustomizer() {
        return configuration -> {
            // 不使用存储过程，不存在多数据集返回
            configuration.setMultipleResultSetsEnabled(false);
            // 配置打印sql语句
            configuration.setLogImpl(StdOutImpl.class);
//            configuration.setLogImpl(Slf4jImpl.class);
        };
    }

    @Bean
    public MybatisPlusPropertiesCustomizer jdbcPropertiesCustomizer() {
        return properties -> {
            // 不打印 MybatisPlus 的 Banner
            properties.getGlobalConfig().setBanner(false);
            // 全局设置 ID 生成策略为雪花算法
            properties.getGlobalConfig().getDbConfig().setIdType(IdType.ASSIGN_ID);
        };
    }

}
